package com.crysta1.aitrans;

import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.SwitchCompat;

public class SettingsActivity extends AppCompatActivity {

    private EditText etApiKey;
    private Spinner spinnerDifficulty; // 确保声明了这个变量
    private SwitchCompat switchNightMode;
    private Button btnSave;
    private String selectedDifficulty = "M";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 初始化视图
        initViews();

        // 加载保存的设置
        loadSettings();

        // 设置监听器
        setListeners();
    }

    private void initViews() {
        etApiKey = findViewById(R.id.etApiKey);
        spinnerDifficulty = findViewById(R.id.spinnerDifficulty); // 确保初始化了这个变量
        switchNightMode = findViewById(R.id.switchNightMode);
        btnSave = findViewById(R.id.btnSave);

        // 设置难度下拉选项
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this,
                R.array.difficulty_options,
                android.R.layout.simple_spinner_item
        );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDifficulty.setAdapter(adapter);
    }

    private void loadSettings() {
        SharedPreferences prefs = getSharedPreferences("settings", MODE_PRIVATE);
        String apiKey = prefs.getString("api_key", "");
        String difficulty = prefs.getString("difficulty", "M");
        boolean nightMode = prefs.getBoolean("night_mode", false);

        etApiKey.setText(apiKey);

        // 设置选中的难度
        String[] difficultyOptions = getResources().getStringArray(R.array.difficulty_options);
        for (int i = 0; i < difficultyOptions.length; i++) {
            if (difficultyOptions[i].startsWith(difficulty)) {
                spinnerDifficulty.setSelection(i);
                selectedDifficulty = difficulty;
                break;
            }
        }

        // 设置夜间模式开关状态
        switchNightMode.setChecked(nightMode);
    }

    private void setListeners() {
        spinnerDifficulty.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selected = parent.getItemAtPosition(position).toString();
                selectedDifficulty = selected.substring(0, 1); // 获取第一个字符(E/M/H/R)
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 什么都不做
            }
        });

        switchNightMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 立即应用夜间模式设置
                AppCompatDelegate.setDefaultNightMode(
                        isChecked ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
                );
            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveSettings();
            }
        });
    }

    private void saveSettings() {
        String apiKey = etApiKey.getText().toString().trim();

        SharedPreferences prefs = getSharedPreferences("settings", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("api_key", apiKey);
        editor.putString("difficulty", selectedDifficulty);
        editor.putBoolean("night_mode", switchNightMode.isChecked());
        editor.apply();

        Toast.makeText(this, "设置已保存", Toast.LENGTH_SHORT).show();
        finish();
    }
}